﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsAzure;

namespace AzureLogViewer
{
    public partial class AzureLogViewer : Form
    {
        public AzureLogViewer()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            txtResults.Text = string.Empty;
            txtMessage.Text = string.Empty;
            var accountName = txtAccountName.Text;// "slv";
            var sharedKey = txtAccountKey.Text;// "xreonsqDRcpHaUBe0VrcgyzdAoBrNFFnu++ebohm70jVDyDWsEte7eAVtfp2EzOYRPo05qu2SW9lUC5ulXNShQ==";
            var account = new CloudStorageAccount(
                new StorageCredentialsAccountAndKey(accountName, sharedKey), true);
            //var account = CloudStorageAccount.DevelopmentStorageAccount;

            var dt = Convert.ToDateTime(dtStartDate.Text);// new DateTime(2012, 4, 18, 7, 0, 0); // Logs more recent than 1/28/2010 4PM
            var dt2 = Convert.ToDateTime(dtEndDate.Text);// new DateTime(2012, 4, 18, 10, 0, 0); // Logs more recent than 1/28/2010 4PM
            string deploymentId = txtDeploymentId.Text;
            string activityId = txtActivityId.Text;
            var wadlog = new WADLogsTableContext(account.TableEndpoint.ToString(), account.Credentials);
            var query = from row in wadlog.WADLogsTable
                        where string.Compare(row.PartitionKey, "0" + dt.Ticks.ToString()) > 0 && string.Compare(row.PartitionKey, "0" + dt2.Ticks.ToString()) < 0 && row.DeploymentId == deploymentId
                        select row;

            //query.Where(r => r.Message.Contains("a")).Dump();
            //var rows = query.Where(r => r.Message.Contains("8b28cf48-b639-430a-b5b8-ef6c18eb3e75"));
            //var rows = query.Where(r => r.DeploymentId == "b8c5a808be4b48ad85afabb0bc0c0f1d");
            try
            {
                List<WADLogsTable> logs = query.ToList<WADLogsTable>();
                StringBuilder results = new StringBuilder("");
                foreach (var row in logs)
                {
                    if (row.Message.Contains(activityId))
                    {
                        results.AppendLine(row.Message);
                        results.AppendLine("=======================================================================");

                    }
                    //Console.WriteLine(row.Message);
                }

                if (String.IsNullOrEmpty(results.ToString()))
                    txtResults.Text = "No logs found";
                else
                    txtResults.Text = results.ToString();

            }
            catch (Exception ex)
            {
                txtMessage.Text = ex.ToString();
            }
            

        }

        private void btnClearResults_Click(object sender, EventArgs e)
        {
            txtResults.Text = string.Empty;
            txtMessage.Text = string.Empty;
        }
    }
}
